<template>
	<view>
		<view class="banmi_flex a-c j-c">
			<view :class="order_index==index?'order_name_selected':'order_name'" v-for="(item,index) in order_list"
				:key="index" @click="on_order(index,item)">
				<text class="f-14">{{item.name}}</text>
			</view>
		</view>
	</view>
</template>

<script>
	var now = new Date(); //当前日期
	var nowDayOfWeek = now.getDay(); //今天本周的第几天
	var nowDay = now.getDate(); //当前日
	var nowMonth = now.getMonth(); //当前月
	var nowYear = now.getYear(); //当前年
	nowYear += (nowYear < 2000) ? 1900 : 0; //

	var lastMonthDate = new Date(); //上月日期
	lastMonthDate.setDate(1);
	lastMonthDate.setMonth(lastMonthDate.getMonth() - 1);
	var lastYear = lastMonthDate.getYear();
	var lastMonth = lastMonthDate.getMonth();
	export default {
		name: '',
		components: {

		},
		props: {
			value: {
				type: [Number, String, Boolean],
				default: -1,
			},
			type: {
				type: [Number, String, Boolean],
				default: '',
			},
		},
		data() {
			return {
				order_index: -1,
				find: {},
				order_list: [{
						'name': '今天',
						'day': 1

					},
					{
						'name': '昨天',
						'day': 2

					},
					{
						'name': '近30天',
						'day': 30

					},
					{
						'name': '近半年',
						'day': 180

					},
					{
						'name': '近一年',
						'day': 365

					},
					{
						'name': '本月',
						'day': '本月'

					},
					{
						'name': '当季',
						'day': '当季'

					},
					{
						'name': '本年',
						'day': '本年'

					}
				],
				order_list_end: [{
						'name': '1天',
						'day': 1

					},
					{
						'name': '7天',
						'day': 7

					},
					{
						'name': '30天',
						'day': 30

					},
					{
						'name': '180天',
						'day': 180

					},
					{
						'name': '一年',
						'day': 367

					},
					{
						'name': '永久',
						'day': 36599

					}


				],

			}
		},
		mounted() {
			if (this.type == 'end') {
				this.order_list = this.order_list_end
			}
			this.order_index = this.value

		},
		methods: {
			on_order(index, item) {
				//今日开始时间
				var time = new Date(new Date().toLocaleDateString()).getTime() / 1000;

				this.find.name = item.name
				this.find.order_index = index
				switch (item.day) {
					case 1:
						this.find.start_time = time;
						this.find.end_time = time + 86399;

						break;
					case 2:
						this.find.start_time = time - 86400;
						this.find.end_time = this.find.start_time + 86399;
						break;
					case '本月':
						this.find = this.y_end_day();
						break;
					case '本年':
						this.find = this.Year_end();
						break;
					case '当季':
						this.find = this.getQuarterStartDate();
						break;
					default:
						this.find.start_time = time - item.day * 86400
						this.find.end_time = time + 86399;
						break;
				}
				this.find.e_time = time + 86400 * item.day ;
				this.order_index = index;

				this.$emit('change', {
					find: this.find
				})


			},

			getQuarterStartDate() {


				//获得本季度的开端日期
				var quarterStartDate = new Date(nowYear, this.getQuarterStartMonth(), 1);
				var b = this.formatDateTime(quarterStartDate) + ' 00:00:00';
				this.find.start_time = Date.parse(b) / 1000;

				//获得本季度的结束日期
				var quarterEndMonth = this.getQuarterStartMonth() + 2;
				var quarterStartDate = new Date(nowYear, quarterEndMonth, this.getMonthDays(quarterEndMonth));
				var end = this.formatDate(quarterStartDate) + ' 23:59:59';

				this.find.end_time = Date.parse(end) / 1000;
				return this.find;

			},
			//格局化日期：yyyy-MM-dd
			formatDate(date) {
				var myyear = date.getFullYear();
				var mymonth = date.getMonth() + 1;
				var myweekday = date.getDate();

				if (mymonth < 10) {
					mymonth = "0" + mymonth;
				}
				if (myweekday < 10) {
					myweekday = "0" + myweekday;
				}
				return (myyear + "-" + mymonth + "-" + myweekday);
			},
			//获得某月的天数
			getMonthDays(myMonth) {
				var monthStartDate = new Date(nowYear, myMonth, 1);
				var monthEndDate = new Date(nowYear, myMonth + 1, 1);
				var days = (monthEndDate - monthStartDate) / (1000 * 60 * 60 * 24);
				return days;
			},

			//获得本季度的开端月份
			getQuarterStartMonth() {
				var quarterStartMonth = 0;
				if (nowMonth < 3) {
					quarterStartMonth = 0;
				}
				if (2 < nowMonth && nowMonth < 6) {
					quarterStartMonth = 3;
				}
				if (5 < nowMonth && nowMonth < 9) {
					quarterStartMonth = 6;
				}
				if (nowMonth > 8) {
					quarterStartMonth = 9;
				}
				return quarterStartMonth;
			},
			formatDateTime(inputTime, type) {
				var date = new Date(inputTime);
				var y = date.getFullYear();
				var m = date.getMonth() + 1;
				m = m < 10 ? ('0' + m) : m;
				var d = date.getDate();
				d = d < 10 ? ('0' + d) : d;
				var h = date.getHours();
				h = h < 10 ? ('0' + h) : h;
				var minute = date.getMinutes();
				var second = date.getSeconds();
				minute = minute < 10 ? ('0' + minute) : minute;
				second = second < 10 ? ('0' + second) : second;
				if (type) {
					return y + '-' + m + '-' + d + ' ' + h + ':' + minute + ':' + second; //2017-12-12 12:23:34    
				} else {
					return y + '-' + m + '-' + d; //2017-12-12
				}
			},

			//今年开始 今年结束
			Year_end() {
				//今年开始
				this.find.start_time = Date.parse(new Date(new Date().getFullYear(), 0, 1)) / 1000;
				//今年结束
				this.find.end_time = Date.parse(new Date(new Date(new Date().getFullYear() + 1, 0, 0).setHours(23, 59, 59,
					59))) / 1000
				return this.find;

			},
			//本月开始 最后1天
			y_end_day() {
				//本月开始时间
				const date = new Date()
				date.setDate(1)
				date.setHours(0, 0, 0, 0)
				var timeStamp = date.getTime() / 1000
				this.find.start_time = timeStamp;
				//本月结束时间
				var end_time = Date.parse(new Date(new Date(new Date().getFullYear(), new Date().getMonth() + 1, 0)
					.setHours(23, 59, 59, 59)))
				this.find.end_time = end_time / 1000;

				return this.find;
			},



			close(type) {

				this.$emit('change', {
					show: false,
					type: this.type
				})

			},


		}
	}
</script>
<style lang="scss">
	.order_name {
		padding: 23rpx 25rpx;
		border: solid 2rpx #DCDCDC;
		color: #808080;
	}

	.order_name_selected {
		padding: 23rpx 25rpx;
		border: solid 2rpx #007AFF;

		color: #007AFF;
	}
</style>